﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using SIMS.BL;
using MandalaySoft.Common;
using SIMS.BL.Common;
/// <summary>
/// Summary description for General
/// </summary>
public class General
{
    public General()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static void BindSchoolNameList(DropDownList ddlSchool, string filter, bool IsFullName, out MyError error)
    {
        DataTable _dt = new DataTable();
        SchoolDB _db = new SchoolDB();
        error = new MyError();
        _dt = _db.GetSchoolNameList(filter, IsFullName, out error);
        ddlSchool.Items.Clear();
        if (error.ErrorNo == 0)
        {
            if (_dt != null && _dt.Rows.Count > 0)
            {
                foreach (DataRow _dr in _dt.Rows)
                {
                    ddlSchool.Items.Add(new ListItem(_dr["SchoolName"].ToString(), _dr["SchoolID"].ToString()));
                }
            }
        }
    }

    public static void BindUserProfileList(DropDownList ddlUserProfile)
    {
        DataTable _dt = new DataTable();
        Listing _list = new Listing();
        _dt = _list.GetUserProfileList();
        
        if (_dt != null && _dt.Rows.Count > 0)
        {
            foreach (DataRow _dr in _dt.Rows)
            {
                ddlUserProfile.Items.Add(new ListItem(_dr["ProfileName"].ToString(), _dr["UserProfileID"].ToString()));
            }
        }
        
    }

    public static void BindGradeLevelList(DropDownList ddlGradeLevel, int SchoolID, string filter, bool IsFullName, out MyError error)
    {
        DataTable _dt = new DataTable();
        GradeLevelDB _db = new GradeLevelDB();
        error = new MyError();
        _dt = _db.GetGradeLevelList(filter, SchoolID, IsFullName, out error);
        ddlGradeLevel.Items.Clear();
        if (error.ErrorNo == 0)
        {
            if (_dt != null && _dt.Rows.Count > 0)
            {
                foreach (DataRow _dr in _dt.Rows)
                {
                    ddlGradeLevel.Items.Add(new ListItem(_dr["GradeLevelName"].ToString(), _dr["GradeLevelID"].ToString()));
                }
            }
        }
    }

    public static string ExportToExcel(GridView gvMain, string headerText)
    {
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            HtmlForm frm = new HtmlForm();
            GridView gvTeamp = new GridView();
            gvTeamp = gvMain;
            frm.InnerHtml = "<table><tr><td colspan='10'><h2>" + headerText + "</h2></td></tr></table>";
            gvTeamp.HeaderRow.BackColor = System.Drawing.Color.LightGray;
            gvTeamp.HeaderRow.Font.Bold = true;
            gvTeamp.HeaderRow.ForeColor = System.Drawing.Color.Black;

            for (int h = 0; h < gvTeamp.HeaderRow.Cells.Count; h++)
            {
                if (gvTeamp.Columns[h].ControlStyle.CssClass == "hidden")
                {
                    gvTeamp.HeaderRow.Cells[h].Visible = false;
                }
            }

            for (int i = 0; i < gvTeamp.Rows.Count; i++)
            {
                GridViewRow row = gvTeamp.Rows[i];

                for (int c = 0; c < row.Cells.Count; c++)
                {
                    if (gvTeamp.Columns[c].ControlStyle.CssClass == "hidden")
                    {
                        row.Cells[c].Visible = false;
                    }
                }

                row.BackColor = System.Drawing.Color.White;
                row.Attributes.Add("class", "textmode");
            }

            gvTeamp.Parent.Controls.Add(frm);

            frm.Attributes["runat"] = "server";
            frm.Controls.Add(gvTeamp);
            frm.RenderControl(hw);
            return sw.ToString();
        }
    }

    public static void CreateUpdateUserLoginCached(System.Web.UI.Page thisPage)
    {
        string sKey = Global.Session_UserCacheID;
        sKey += Convert.ToString(thisPage.Session[Global.Session_UserID].ToString());
        // Accessing the Cache Item extends the Sliding Expiration automatically
        if (HttpContext.Current.Cache[sKey] != null)
        {
            //string sUser = HttpContext.Current.Cache[sKey].ToString();
        }
        else
        {
            TimeSpan SessTimeOut = new TimeSpan(0, 0, thisPage.Session.Timeout, 0, 0);
            HttpContext.Current.Cache.Insert(sKey, thisPage.Session[Global.Session_UserID].ToString(), null, DateTime.MaxValue, SessTimeOut,
               System.Web.Caching.CacheItemPriority.NotRemovable, null);
        }
    }

    public static bool ClearUserLoginCached(string UserId)
    {
        bool _retValue = false;
        string sKey = Global.Session_UserCacheID;
        sKey += UserId;

        if (HttpContext.Current.Cache[sKey] == null)
        {
            _retValue = true;
        }
        else
        {
            HttpContext.Current.Cache.Remove(sKey);
            _retValue = true;
        }

        return _retValue;
    }

    public static bool CheckUserLoginCached(string UserId)
    {
        bool _retValue = false;
        string sKey = Global.Session_UserCacheID;
        sKey += UserId;
        if (HttpContext.Current.Cache[sKey] == null)
        {
            _retValue = true;
        }
        else
        {
            _retValue = false;
        }
        return _retValue;
    }

    public static string removeHTMLTags(string inputHTML)
    {
        return Regex.Replace
          (inputHTML, "<.*?>", string.Empty);
    }

    public static string GetSiteRootUrl()
    {
        string protocol;

        if (HttpContext.Current.Request.IsSecureConnection)
            protocol = "https";
        else
            protocol = "http";

        StringBuilder uri = new StringBuilder(protocol + "://");

        string hostname = HttpContext.Current.Request.Url.Host;

        uri.Append(hostname);

        int port = HttpContext.Current.Request.Url.Port;

        if (port != 80 && port != 443)
        {
            uri.Append(":");
            uri.Append(port.ToString());
        }

        return uri.ToString();
    }
}